package svn;

import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.wc.*;

import java.io.File;

/**
 * @Author 史祥连
 * @Date 2023/12/7 13:29
 * @Version 1.0
 */
public class SVNUtil {
    private static ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager("shxl2021", "ff8d7o0rmc2j7");

    public static ISVNAuthenticationManager getAuthManager() {
        return authManager;
    }

    public static SVNClientManager getClientManager() {
        return SVNClientManager.newInstance((ISVNOptions) null, authManager);
    }

    public static void add(String projectPath) throws SVNException {
        File repositoryFile = new File(projectPath);
        SVNClientManager clientManager = getClientManager();
        SVNStatusClient statusClient = clientManager.getStatusClient();
        statusClient.doStatus(repositoryFile, SVNRevision.BASE, SVNDepth.INFINITY, false, false, true, false,
                new ISVNStatusHandler() {
                    @Override
                    public void handleStatus(SVNStatus status) throws SVNException {
                        // 获取未受版本控制的文件
                        if (status.getContentsStatus() == SVNStatusType.STATUS_NONE) {
                            // 添加未被版本控制的文件到SVN
                            clientManager.getWCClient().doAdd(status.getFile(), false, false, false, SVNDepth.INFINITY, false, false, true);
                            System.out.println("Added: " + status.getFile());
                        }
                    }
                }, null);
    }

    public static void commit(String projectPath, String commitMsg) {
        File repositoryFile = new File(projectPath);

        try {
            // 创建SVNClientManager实例
            SVNClientManager clientManager = SVNClientManager.newInstance((ISVNOptions) null, authManager);

            // 创建SVNCommitClient实例
            SVNCommitClient commitClient = clientManager.getCommitClient();

            // 提交文件到SVN服务器
             commitClient.doCommit(new File[]{repositoryFile}, true, commitMsg, null, null, false, false, SVNDepth.INFINITY);
        } catch (SVNException e) {
            e.printStackTrace();
        }
    }

    public static String checkout(String svnLocation) {
        String[] split = svnLocation.split("/");
        String proPath = "C:\\Datas\\E9\\客户分支\\" + split[split.length - 1];
        // 本地SVN仓库路径（目标路径）
        File localRepositoryDir = new File(proPath);
        // SVN服务器URL
        String svnUrl = svnLocation; // 替换为您的SVN服务器URL

        try {
            SVNURL svnRepositoryURL = SVNURL.parseURIDecoded(svnUrl);

            // 创建SVNClientManager实例
            SVNClientManager clientManager = getClientManager();

            // 创建SVNUpdateClient实例
            SVNUpdateClient updateClient = clientManager.getUpdateClient();

            // 拉取远程SVN仓库到本地
            updateClient.doCheckout(svnRepositoryURL, localRepositoryDir, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, false);

            System.out.println("Repository checked out to: " + localRepositoryDir.getAbsolutePath());
        } catch (SVNException e) {
            e.printStackTrace();
        }
        return proPath;
    }
}
